package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._

object Demo4CountWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordDS: DataStream[String] = linesDS.flatMap(_.split(","))

    val kvDS: DataStream[(String, Int)] = wordDS.map((_, 1))

    /**
     * countWindow(10): 滚动的统计窗口，每隔几条数据计算一次
     * countWindow(10, 2): 滑动的统计窗口
     */
    kvDS
      .keyBy(_._1)
      .countWindow(10, 2)
      .sum(1)
      .print()


    env.execute()

  }

}
